﻿Public Class ClsTEmployeeDbAccess
    Inherits ClsBaseDbAccess

    Public Sub New(ByVal s As String)
        MyBase.New(s, TABLE_NAME_T_EMPLOYEE)
    End Sub

    ''' <summary>
    ''' 两个参数，s - 连接字串；n - 表名
    ''' </summary>
    ''' <param name="s"></param>
    ''' <param name="n"></param>
    Public Sub New(ByVal s As String, ByVal n As String)
        MyBase.New(s, n)
    End Sub


    Public Function InsertEmployee(ByVal tmpCls As ClsTEmployee) As Integer
        Try

            Dim tmpSql As String
            Dim tmpDbAccess = GetConnection()

            Dim tmpParas(4) As OleDb.OleDbParameter
            Dim tmpReturn As Integer

            '打开数据库
            tmpDbAccess.DbOpen()

            '构造查询语句
            tmpSql = "insert into [" & Me.AppTableName & "] ("
            tmpSql &= "[EmployeeId],[EmployeeCode],[DepartmentId],[departmentName],[EmployeeName],[Contact]"
            tmpSql &= ") values("
            tmpSql &= "@EmployeeId,@EmployeeCode,@DepartmentId,@departmentName,@EmployeeName,@Contact"
            tmpSql &= ")"

            '构造参数集合
            tmpParas(0) = New OleDb.OleDbParameter("@EmployeeId", OleDb.OleDbType.Integer)
            tmpParas(1) = New OleDb.OleDbParameter("@EmployeeCode", OleDb.OleDbType.VarWChar)
            tmpParas(2) = New OleDb.OleDbParameter("@DepartmentId", OleDb.OleDbType.Integer)
            tmpParas(3) = New OleDb.OleDbParameter("@departmentName", OleDb.OleDbType.VarWChar)
            tmpParas(4) = New OleDb.OleDbParameter("@EmployeeName", OleDb.OleDbType.VarWChar)
            tmpParas(5) = New OleDb.OleDbParameter("@Contact", OleDb.OleDbType.VarWChar)

            '为查询参数赋值
            tmpParas(0).Value = tmpCls.EmployeeId
            tmpParas(1).Value = tmpCls.EmployeeCode
            tmpParas(2).Value = tmpCls.DepartmentId
            tmpParas(3).Value = tmpCls.EmployeeName
            tmpParas(4).Value = tmpCls.Contact

            '执行更新查询(插入、修改、删除)
            tmpReturn = tmpDbAccess.InsertData(tmpSql, tmpParas, True)

            '关闭数据库
            tmpDbAccess.DbClose()

            Return tmpReturn

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTEmployeeDbAccess - InsertEmployee"

            Return 0
        End Try
    End Function

    Public Overrides Function ConvertRecordToClass(reader As OleDb.OleDbDataReader) As Object
        If reader Is Nothing Then
            Return Nothing
        End If

        Dim tmpList As List(Of ClsTEmployee) = New List(Of ClsTEmployee)
        Try

            Dim tmpCls As ClsTEmployee

            While reader.Read()

                tmpCls = New ClsTEmployee()

                With tmpCls
                    .EmployeeId = reader.GetInt32(0)
                    .EmployeeCode = ConvertDbNullString(reader(1))
                    .DepartmentId = reader.GetInt32(2)
                    .DepartmentName = ConvertDbNullString(reader(3))
                    .EmployeeName = ConvertDbNullString(reader(4))
                    .Contact = ConvertDbNullString(reader(5))
                End With

                tmpList.Add(tmpCls)

            End While


        Catch ex As Exception

            mErrorNumber = -1001
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTEmployeeDbAccess - ConvertRecordToClass"

            'Return Nothing
        End Try

        Return tmpList
    End Function
End Class
